from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import users, categorys, orders, likes, goods, comments, collects
import json
from django.shortcuts import render
from django.core import serializers
from django.core.paginator import Paginator
from django.conf import settings
from django.utils import timezone
from django.db.models import Q, Sum, Count
import os
# 和前端约定的返回格式


def initres():
    return {"message": 'success', "code": '0', "data": []}

# JSON请求参数转换


def convert_json(data_bytes):
    data_str = data_bytes.decode()
    data_json = eval(json.loads(json.dumps(data_str), strict=False))
    return data_json

# 分页参数


def getPage(lists, request):
    pageSize = request.GET.get('pageSize', 10)
    currPage = request.GET.get('currPage', 1)

    paginator = Paginator(lists, pageSize)
    return paginator.page(currPage).object_list, paginator.count

# 查询全部用户数据信息


def getUserList(request):
    result = initres()
    name = request.GET.get('name', '')
    id = request.GET.get('id', '')
    student_id = request.GET.get('student_id', '')
    class_name = request.GET.get('class_name', '')
    lists = users.objects.filter(name__contains=name, id__contains=id, student_id__contains=student_id, class_name__contains=class_name).values(
        'id', 'name', 'student_id', 'password', 'age', 'student_img', 'phone', 'class_name', 'state', 'create_time', 'update_time')
    print(id, lists)
    newLists, count = getPage(lists, request)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)

# 登录接口


def login(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        name = data_json.get('name', '')
        password = data_json.get('password', '')
        lists = users.objects.filter(name=name).values()
        if (lists.exists()):
            sqlpassword = lists[0]['password']

            if (sqlpassword == password):
                state = lists[0]['state']
                if (state == '0'):
                    result["code"] = 100003
                    result["message"] = '账号被禁用'
                    return JsonResponse(result, content_type="application/json")
                else:
                    result["data"] = list(lists)
                    return JsonResponse(result, content_type="application/json")
            else:
                result["code"] = 100002
                result["message"] = '密码错误'
                return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 100001
            result["message"] = '用户名不存在'
            return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 注册接口


def register(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        name = data_json.get('name', '')
        password = data_json.get('password', '')
        student_id = data_json.get('student_id', '')
        phone = data_json.get('phone', '')
        state = data_json.get('state', '1')
        lists = users.objects.filter(name=name).values()

        if (lists.exists() == False):
            lists2 = users.objects.filter(phone=phone).values()
            if (lists2.exists() == False):
                lists3 = users.objects.filter(student_id=student_id).values()
                if (lists3.exists() == False):
                    users.objects.create(
                        name=name, password=password, student_id=student_id, phone=phone, state=state)
                    return JsonResponse(result, content_type="application/json")
                else:
                    result["code"] = 100002
                    result["message"] = '学号已注册'
                    return JsonResponse(result, content_type="application/json")
            else:
                result["code"] = 100002
                result["message"] = '手机号已注册'
                return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 100001
            result["message"] = '用户名已存在'
            return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 修改用户接口


def modifyUsers(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        name = data_json.get('name', '')
        phone = data_json.get('phone', '')
        lists = users.objects.filter(id=id).values()
        lists2 = users.objects.filter(name=name).values()
        print(lists[0]['name'] != name, lists2.exists())

        if ((lists[0]['name'] == name and lists2.exists()) or (lists[0]['name'] != name and lists2.exists() == False)):
            lists3 = users.objects.filter(phone=phone).values()
            if ((lists[0]['phone'] == phone and lists3.exists()) or (lists[0]['phone'] != phone and lists3.exists() == False)):
                old = users.objects.filter(id=id).values()
                oldname = data_json.get('name', old[0]['name'])
                oldphone = data_json.get('phone', old[0]['phone'])
                password = data_json.get('password', old[0]['password'])
                age = data_json.get('age', old[0]['age'])
                student_img = data_json.get(
                    'student_img', old[0]['student_img'])
                class_name = data_json.get('class_name', old[0]['class_name'])
                state = data_json.get('state', old[0]['state'])
                users.objects.filter(id=id).update(class_name=class_name, name=oldname,
                                                   password=password, age=age, student_img=student_img, phone=oldphone, state=state)
                return JsonResponse(result, content_type="application/json")
            else:
                result["code"] = 100002
                result["message"] = '手机号已存在'
                return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 100001
            result["message"] = '用户名已存在'
            return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 修改密码


def changepassword(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        password = data_json.get('password', '')
        newpassword = data_json.get('newpassword', '')
        lists = users.objects.filter(id=id).values()
        if (lists[0]['password'] == password):
            users.objects.filter(id=id).update(password=newpassword)
            return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 100002
            result["message"] = '密码输入错误'
            return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 上传图片


def upload(request):
    result = initres()
    file = request.FILES.get('file')
    print('file', file.name)
    img_path = settings.MEDIA_ROOT+'\\'+file.name
    with open(img_path, 'ab') as fp:
        for chunk in file.chunks():
            fp.write(chunk)
    result["data"] = '/media/'+file.name
    return JsonResponse(result)

# 查询全部类别数据信息


def getCategoryNameList(request):
    result = initres()
    category_name = request.GET.get('category_name', '')
    lists = categorys.objects.filter(category_name__contains=category_name).values(
        'id', 'category_name', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)

# 添加类别数据信息


def addCategory(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        category_name = data_json.get('category_name', '')
        categorys.objects.create(category_name=category_name)
        return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 修改类别数据信息


def updateCategory(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        category_name = data_json.get('category_name', '')
        categorys.objects.filter(id=id).update(category_name=category_name)
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 删除类别数据信息


def deletCategory(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        categorys.objects.filter(id=id).delete()
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 查询全部点赞数据信息


def getLikeList(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    goods_id = request.GET.get('goods_id', '')
    lists = likes.objects.filter(user_id__contains=user_id, goods_id__contains=goods_id).values(
        'id', 'user_id', 'goods_id', 'is_like', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    for index in range(len(newLists)):
        goodlist = goods.objects.filter(
            id=newLists[index]['goods_id']).values()
        userlist = users.objects.filter(id=goodlist[0]['user_id']).values()
        newLists[index]['userlist'] = list(userlist)
        newLists[index]['goodlist'] = list(goodlist)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)

# 点赞


def addLike(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        user_id = data_json.get('user_id', '')
        goods_id = data_json.get('goods_id', '')
        lists = likes.objects.filter(user_id=user_id, goods_id=goods_id)
        if (lists.exists() == False):
            likes.objects.create(user_id=user_id, goods_id=goods_id)
            return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 10001
            result["message"] = '数据已存在'
            return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 取消点赞


def deletLike(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        user_id = data_json.get('user_id', '')
        goods_id = data_json.get('goods_id', '')
        lists = likes.objects.filter(user_id=user_id, goods_id=goods_id)
        id = list(lists)[0].id
        likes.objects.filter(id=id).delete()
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 查询点赞数


def getLikenum(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    goods_id = request.GET.get('goods_id', '')
    count = likes.objects.filter(
        user_id__contains=user_id, goods_id__contains=goods_id).count()
    result["data"] = count
    return JsonResponse(result)

# 查询全部商品数据信息


def getGoodList(request):
    result = initres()
    name = request.GET.get('name', '')
    id = request.GET.get('id', '')
    category_id = request.GET.get('category_id', '')
    state = request.GET.get('state', '')
    review_status = request.GET.get('review_status', '')
    user_id = request.GET.get('user_id', '')
    lists = goods.objects.filter(id__contains=id, user_id__contains=user_id, name__contains=name, category_id__contains=category_id, state__contains=state, review_status__contains=review_status).values(
        'id', 'name', 'goods_img', 'price', 'category_id', 'release_time', 'user_id', 'click_count', 'state', 'fineness', 'is_delet', 'review_status', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    for index in range(len(newLists)):
        userlist = users.objects.filter(id=newLists[index]['user_id']).values()
        categorylist = categorys.objects.filter(
            id=newLists[index]['category_id']).values()
        newLists[index]['userlist'] = list(userlist)
        newLists[index]['categorylist'] = list(categorylist)
        collectscount = collects.objects.filter(
            goods_id__contains=newLists[index]['id']).count()
        newLists[index]['collectscount'] = collectscount

        commentscount = comments.objects.filter(
            goods_id__contains=newLists[index]['id']).count()
        newLists[index]['commentscount'] = commentscount

        likescount = likes.objects.filter(
            goods_id__contains=newLists[index]['id']).count()
        newLists[index]['likescount'] = likescount

    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)

# 查询全部收藏数据信息


def getCollectList(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    goods_id = request.GET.get('goods_id', '')
    lists = collects.objects.filter(user_id__contains=user_id, goods_id__contains=goods_id).values(
        'id', 'user_id', 'goods_id', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    for index in range(len(newLists)):
        goodlist = goods.objects.filter(
            id=newLists[index]['goods_id']).values()
        userlist = users.objects.filter(id=goodlist[0]['user_id']).values()
        newLists[index]['userlist'] = list(userlist)
        newLists[index]['goodlist'] = list(goodlist)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)

# 收藏


def addCollect(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        user_id = data_json.get('user_id', '')
        goods_id = data_json.get('goods_id', '')
        lists = collects.objects.filter(user_id=user_id, goods_id=goods_id)
        if (lists.exists() == False):
            collects.objects.create(user_id=user_id, goods_id=goods_id)
            return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 10001
            result["message"] = '数据已存在'
            return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 取消收藏


def deletCollect(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        user_id = data_json.get('user_id', '')
        goods_id = data_json.get('goods_id', '')
        lists = collects.objects.filter(user_id=user_id, goods_id=goods_id)
        id = list(lists)[0].id
        collects.objects.filter(id=id).delete()
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 查询收藏数


def getCollectnum(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    goods_id = request.GET.get('goods_id', '')
    count = collects.objects.filter(user_id__contains=user_id, goods_id__contains=goods_id).count()
    result["data"] = count
    return JsonResponse(result)

# 查询全部评论数据信息


def getCommentList(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    goods_id = request.GET.get('goods_id', '')
    two_id = request.GET.get('two_id', '')
    print('two_id', two_id, 'user_id', user_id, 'goods_id', goods_id)
    lists = comments.objects.filter(user_id__contains=user_id, goods_id__contains=goods_id).values('id', 'content', 'user_id', 'goods_id', 'two_id', 'is_delet', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    print('newLists---------',newLists, count)
    for index in range(len(newLists)):
        userlist = users.objects.filter(id=newLists[index]['user_id']).values()
        twouserlist = users.objects.filter(
            id=newLists[index]['two_id']).values()
        goodlist = goods.objects.filter(
            id=newLists[index]['goods_id']).values()
        newLists[index]['userlist'] = list(userlist)
        newLists[index]['twouserlist'] = list(twouserlist)
        newLists[index]['goodlist'] = list(goodlist)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)

# 评论


def addComment(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        user_id = data_json.get('user_id', '')
        goods_id = data_json.get('goods_id', '')
        content = data_json.get('content', '')
        two_id = data_json.get('two_id', '')

        if (two_id == ''):
            two_id = None

            comments.objects.create(
                two_id=two_id, content=content, user_id=user_id, goods_id=goods_id)
            return JsonResponse(result, content_type="application/json")
        else:
            comments.objects.create(
                two_id=two_id, content=content, user_id=user_id, goods_id=goods_id)
            return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 删除评论


def deletComment(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        comments.objects.filter(id=id).delete()
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 查询评论数


def getCommentnum(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    goods_id = request.GET.get('goods_id', '')
    print('user_id', user_id, 'goods_id', goods_id)
    count = comments.objects.filter(user_id__contains=user_id, goods_id__contains=goods_id).count()
    result["data"] = count
    return JsonResponse(result)

# 查询各个分类数

def categorynum(request):
    result = initres()
    data = goods.objects.values('category_id').annotate(Count('category_id'))
    print(data)
    result["data"] = list(data)
    return JsonResponse(result, content_type="application/json")

# 添加商品数据信息


def addGood(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        name = data_json.get('name', '')
        goods_img = data_json.get('goods_img', '')
        price = data_json.get('price', '')
        category_id = data_json.get('category_id', '')
        release_time = data_json.get('release_time', '')
        user_id = data_json.get('user_id', '')
        fineness = data_json.get('fineness', '')
        print(name, goods_img, price, category_id, release_time)
        goods.objects.create(name=name, goods_img=goods_img, price=price, category_id=category_id,
                             user_id=user_id, click_count=0, state=3, fineness=fineness, review_status=1)
        return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 修改商品数据信息
def updateGood(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        old = goods.objects.filter(id=id).values()
        if (old.exists() == True):
            name = data_json.get('name', old[0]['name'])
            goods_img = data_json.get('goods_img', old[0]['goods_img'])
            price = data_json.get('price', old[0]['price'])
            category_id = data_json.get('category_id', old[0]['category_id'])
            release_time = data_json.get(
                'release_time', old[0]['release_time'])
            if (release_time == ''):
                release_time=None
            user_id = data_json.get('user_id', old[0]['user_id'])
            fineness = data_json.get('fineness', old[0]['fineness'])
            click_count = data_json.get('click_count', old[0]['click_count'])
            state = data_json.get('state', old[0]['state'])
            review_status = data_json.get(
                'review_status', old[0]['review_status'])
            goods.objects.filter(id=id).update(name=name, goods_img=goods_img, price=price, category_id=category_id, release_time=release_time,
                                               user_id=user_id, click_count=click_count, state=state, fineness=fineness, review_status=review_status)
            return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 100001
            result["message"] = '数据不存在'
            return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")


# 添加商品点击数
def addGoodclick(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        old = goods.objects.filter(id=id).values()
        if (old.exists() == True):
            name = data_json.get('name', old[0]['name'])
            goods_img = data_json.get('goods_img', old[0]['goods_img'])
            price = data_json.get('price', old[0]['price'])
            category_id = data_json.get('category_id', old[0]['category_id'])
            release_time = data_json.get(
                'release_time', old[0]['release_time'])
            user_id = data_json.get('user_id', old[0]['user_id'])
            fineness = data_json.get('fineness', old[0]['fineness'])
            click_count = data_json.get('click_count', old[0]['click_count']+1)
            state = data_json.get('state', old[0]['state'])
            review_status = data_json.get(
                'review_status', old[0]['review_status'])
            goods.objects.filter(id=id).update(name=name, goods_img=goods_img, price=price, category_id=category_id, release_time=release_time,
                                               user_id=user_id, click_count=click_count, state=state, fineness=fineness, review_status=review_status)
            return JsonResponse(result, content_type="application/json")
        else:
            result["code"] = 100001
            result["message"] = '数据不存在'
            return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 删除商品数据信息


def deletGood(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        goods.objects.filter(id=id).delete()
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 查询全部订单数据信息


def getOrderList(request):
    result = initres()
    goods_id = request.GET.get('goods_id', '')
    seller_id = request.GET.get('user_id', '')
    buyer_id = request.GET.get('user_id', '')
    state = request.GET.get('state', '')
    order_num = request.GET.get('order_num', '')
    lists = orders.objects.filter(goods_id__contains=goods_id,).filter(  state__contains=state).filter( order_num__contains=order_num).filter(Q(seller_id__contains=seller_id) | Q(buyer_id__contains=buyer_id)).order_by('-create_time').values(
        'id', 'goods_id', 'seller_id', 'buyer_id', 'state', 'days', 'lease_time', 'order_num', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    for index in range(len(newLists)):
        goodlist = goods.objects.filter(
            id=newLists[index]['goods_id']).values()
        sellerlist = users.objects.filter(
            id=newLists[index]['seller_id']).values()
        buyerlist = users.objects.filter(
            id=newLists[index]['buyer_id']).values()
        newLists[index]['goodlist'] = list(goodlist)
        newLists[index]['sellerlist'] = list(sellerlist)
        newLists[index]['buyerlist'] = list(buyerlist)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result, content_type="application/json")

# 添加订单数据信息


def addOrder(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        goods_id = data_json.get('goods_id', '')
        seller_id = data_json.get('seller_id', '')
        buyer_id = data_json.get('buyer_id', '')
        days = data_json.get('days', '')
        lease_time = timezone.localtime().strftime('%Y-%m-%d %H:%M:%S')
        order_num = timezone.localtime().strftime(
            '%Y%m%d%H%M%S%f') + ('%09d' % seller_id)
        list = goods.objects.filter(id=goods_id).values()
        if (list[0]['state'] == 2):
            goods.objects.filter(id=goods_id).update(state=1)
            orders.objects.create(goods_id=goods_id, seller_id=seller_id, buyer_id=buyer_id,
                                  days=days, lease_time=lease_time, state=1, order_num=order_num)
            return JsonResponse(result, content_type="application/json")

        else:
            result["code"] = 100001
            result["message"] = '商品不可租借'
            return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 修改订单数据信息


def updateOrder(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        state = data_json.get('state', '')
        orders.objects.filter(id=id).update(state=state)

        list = orders.objects.filter(id=id).values()
        if state == 4:
            goods.objects.filter(id=list[0]['goods_id']).update(state=2)

        return JsonResponse(result, content_type="application/json")
    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 删除订单数据信息


def deletOrder(request):
    result = initres()
    if request.method == 'POST':
        data_json = convert_json(request.body)
        id = data_json.get('id', '')
        orders.objects.filter(id=id).delete()
        return JsonResponse(result, content_type="application/json")

    else:
        result["code"] = 100103
        result["message"] = '请求方法错误'
        return JsonResponse(result, content_type="application/json")

# 获取租聘订单
def getOrderData(request):
    result = initres()
    user_id = request.GET.get('user_id', '')
    print('user_id', user_id)
    lists = orders.objects.filter(buyer_id=user_id).filter(~Q(state=4)).order_by('-create_time').values(
        'id', 'goods_id', 'seller_id', 'buyer_id', 'state', 'days', 'lease_time', 'order_num', 'create_time', 'update_time')
    newLists, count = getPage(lists, request)
    for index in range(len(newLists)):
        goodlist = goods.objects.filter(
            id=newLists[index]['goods_id']).values()
        userlist = users.objects.filter(id=goodlist[0]['user_id']).values()
        newLists[index]['userlist'] = list(userlist)
        newLists[index]['goodlist'] = list(goodlist)
    result["data"] = list(newLists)
    result["count"] = count
    return JsonResponse(result)